package com.iwhalecloud.uncc.utils;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DataUtil {
    public  static byte[] getMsg(int msgType,long timeStamp,String msgBody){
       // System.out.println("向OMC发送命令："+msgBody);
        byte[] headMsgByte = new byte[9];
        byte[] msgBodyByteArr=msgBody.getBytes();
        ByteUtil.copyto(headMsgByte, ByteUtil.hex2bytes("FFFF"),0,2);
        ByteUtil.copyto(headMsgByte, ByteUtil.toByteArray(msgType,1),2,1);
        ByteUtil.copyto(headMsgByte,ByteUtil.int2bytes((int)timeStamp),3,4);
        ByteUtil.copyto(headMsgByte,ByteUtil.short2bytes(msgBodyByteArr.length),7,2);
        byte[] msgByteArr = new byte[9+msgBodyByteArr.length];
        ByteUtil.copyto(msgByteArr,headMsgByte,0,9);
        byte[] msgbs=ByteUtil.copyto(msgByteArr,msgBodyByteArr,9,msgBodyByteArr.length);
       // System.out.println("向OMC发送请求消息："+hexToSepHexString(msgbs));
       /* byte[] headMsgByte = new byte[8];
        byte[] msgBodyByteArr=msgBody.getBytes();
        ByteUtil.copyto(headMsgByte,new byte[]{-1},0,1);
        ByteUtil.copyto(headMsgByte, ByteUtil.toByteArray(msgType,1),1,1);
        ByteUtil.copyto(headMsgByte,ByteUtil.int2bytes((int)timeStamp),2,4);
        ByteUtil.copyto(headMsgByte,ByteUtil.short2bytes(msgBodyByteArr.length),6,2);
        byte[] msgByteArr = new byte[8+msgBodyByteArr.length];
        ByteUtil.copyto(msgByteArr,headMsgByte,0,8);
        byte[] msgbs=ByteUtil.copyto(msgByteArr,msgBodyByteArr,8,msgBodyByteArr.length);
        System.out.println("向OMC发送请求消息："+hexToSepHexString(msgbs));*/
              return msgbs;
    }
    public static boolean isJson(String content) {
        try {
            JSONObject.parseObject(content);
        } catch (JSONException ex) {
            try {
                JSONObject.parseArray(content);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }
    //MD5(objectType:alarmId:origSeverity:eventTime)
    public static String getFingerprintComparisonStr(String objectType,String alarmId,
                                                     int origSeverity,String eventTime){
        return DigestUtils.md5Hex(String.format("%s:%s:%d:%s",objectType,alarmId,origSeverity,eventTime));
    }

    /**
     * 16进制转换为16进制字符串
     */
    public static String hexToHexString(byte input) {
        String str = input > 15 ? "" : "0";
        str += Integer.toHexString(input);
        if (str.length() > 2) {
            str = str.substring(str.length() - 2);
        }
        return str;
    }

    /**
     * 16进制数组转换为16进制字符串,并且在每字节之间加上'-'字符
     */
    public static String hexToSepHexString(byte[] data) {
        if (data != null) {
            StringBuffer result = new StringBuffer();
            for (int i = 0; i < data.length; i++) {
                result.append(hexToHexString(data[i]));
                if (i < data.length - 1) {
                    result.append("-");
                }
            }
            return result.toString();
        }
        return "";
    }

    public static <T> List<T> deepCopy(List<T> src) {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);

            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            @SuppressWarnings("unchecked")
            List<T> dest = (List<T>) in.readObject();
            return dest;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }
    public static <T>  T  deepCopyObject(T src) {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try {
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            @SuppressWarnings("unchecked")
             T dest = ( T) in.readObject();
            return dest;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }
    /**
     * 复制map对象
     *
     * @param paramsMap 被拷贝对象
     * @param resultMap 拷贝后的对象
     * @explain 将paramsMap中的键值对全部拷贝到resultMap中；
     * paramsMap中的内容不会影响到resultMap（深拷贝）
     */
    public static void mapCopy(Map paramsMap, Map resultMap) {
        if (resultMap == null) resultMap = new HashMap();
        if (paramsMap == null) return;

        Iterator it = paramsMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            Object key = entry.getKey();
            resultMap.put(key, paramsMap.get(key) != null ? paramsMap.get(key) : "");
        }
    }


    public static String getEncoding(String str) {
        String encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s2 = encode;
                return s2;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s3 = encode;
                return s3;
            }
        } catch (Exception exception3) {
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s1 = encode;
                return s1;
            }
        } catch (Exception exception1) {
        }
        encode = "GB2312";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) {
                String s = encode;
                return s;
            }
        } catch (Exception exception) {
        }
        return "";
    }

    public static final String decoding(String str) {
        try {
            String code = getEncoding(str);
            return new String(str.getBytes(code),"GBK");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}
